﻿namespace PowerSong.Export
    {
    using System;
    using System.Collections.Generic;
    using System.IO;
    using PowerSong.SongDatabase;

    /// <summary>
    /// A base class that can be extended to provide data export functionality into a new non-PowerSong database type.
    /// </summary>
    public abstract class BaseExport
        {

        /// <summary>
        /// The source database to export items from.
        /// </summary>
        protected Database Source { get; private set; }

        public delegate void ReportProgressDelegate(int percentage, string status);

        public event ReportProgressDelegate ReportProgress = null;

        public BaseExport(Database sourceDatabase)
            {
            this.Source = sourceDatabase;
            }

        /// <summary>
        /// Exports data from the source database, returning any error or warning messages that get logged
        /// during the process.
        /// </summary>
        /// <returns>Warnings or errors raised by the export process.</returns>
        public string[] ExportData()
            {

            // Create a new list to hold all of the resulting messages
            var result = new List<string>();
            try
                {
                
                // Perform the export
                Export(result);

                }
            catch (Exception ex)
                {
                result.Add("The import failed for the following reason: " + ex.Message);
                }

            // Return the messages
            return result.ToArray();

            }        

        protected string GetSafeFileName(string prospectiveFileName)
            {
            var result = prospectiveFileName;
            foreach (var c in Path.GetInvalidFileNameChars())
                {
                result = result.Replace(c, '-');
                }
            return result;
            }

        protected void SetProgress(int percentage, string status)
            {
            if (ReportProgress != null) ReportProgress(percentage, status);
            }

        protected abstract void Export(List<string> messages);

        }

    }
